<template>
  <div id="app">
    <el-container class="main-container">
      <!-- 头部 -->
      <el-header class="header">
        <div class="header-left">
          <el-icon class="logo-icon" :size="32" color="#409eff">
            <Monitor />
          </el-icon>
          <h1 class="title">Kafka监控工具</h1>
        </div>
        <div class="header-right">
          <el-tag 
            :type="connectionStatus.connected ? 'success' : 'danger'"
            effect="dark"
            size="large"
          >
            {{ connectionStatus.connected ? '已连接' : '未连接' }}
          </el-tag>
        </div>
      </el-header>

      <!-- 主体内容 -->
      <el-container class="content-container">
        <!-- 左侧内容 -->
        <el-main class="main-content">
          <!-- 连接配置 -->
          <ConnectionConfig 
            @connection-changed="onConnectionChanged"
            class="config-section"
          />
          
          <!-- Topic列表 -->
          <TopicList 
            :topics="topics"
            @topic-selected="onTopicSelected"
            class="topic-section"
          />
          
          <!-- 消息发送 -->
          <MessageSender 
            :selectedTopic="selectedTopic"
            :topics="topics"
            @message-sent="onMessageSent"
            class="sender-section"
          />
        </el-main>

        <!-- 右侧日志 -->
        <el-aside class="log-sidebar" width="400px">
          <OperationLogs />
        </el-aside>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { ref, onMounted, onUnmounted } from 'vue'
import { useKafkaStore } from './stores/kafka'
import ConnectionConfig from './components/ConnectionConfig.vue'
import TopicList from './components/TopicList.vue'
import MessageSender from './components/MessageSender.vue'
import OperationLogs from './components/OperationLogs.vue'

export default {
  name: 'App',
  components: {
    ConnectionConfig,
    TopicList,
    MessageSender,
    OperationLogs
  },
  setup() {
    const kafkaStore = useKafkaStore()
    const topics = ref([])
    const selectedTopic = ref(null)
    const connectionStatus = ref({
      connected: false,
      bootstrap_servers: null
    })

    // 连接状态变化处理
    const onConnectionChanged = async (status) => {
      connectionStatus.value = status
      if (status.connected) {
        await loadTopics()
      } else {
        topics.value = []
        selectedTopic.value = null
      }
    }

    // 加载Topic列表
    const loadTopics = async () => {
      try {
        const response = await kafkaStore.getTopics()
        if (response.success) {
          topics.value = response.data
        }
      } catch (error) {
        console.error('加载Topic失败:', error)
      }
    }

    // Topic选择处理
    const onTopicSelected = (topic) => {
      selectedTopic.value = topic
    }

    // 消息发送处理
    const onMessageSent = (result) => {
      // 消息发送完成，可以在这里处理结果
      console.log('消息发送结果:', result)
    }

    // 检查连接状态
    const checkConnectionStatus = async () => {
      try {
        const response = await kafkaStore.getConnectionStatus()
        if (response.success) {
          connectionStatus.value = response.data
          if (response.data.connected) {
            await loadTopics()
          }
        }
      } catch (error) {
        console.error('检查连接状态失败:', error)
      }
    }

    onMounted(() => {
      checkConnectionStatus()
    })

    return {
      topics,
      selectedTopic,
      connectionStatus,
      onConnectionChanged,
      onTopicSelected,
      onMessageSent
    }
  }
}
</script>

<style scoped>
.main-container {
  height: 100vh;
  background-color: #f5f7fa;
}

.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  padding: 8px;
}

.title {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
}

.header-right {
  display: flex;
  align-items: center;
}

.content-container {
  height: calc(100vh - 60px);
  gap: 0;
}

.main-content {
  background-color: #ffffff;
  margin: 16px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow-y: auto;
}

.config-section {
  margin-bottom: 20px;
}

.topic-section {
  margin-bottom: 20px;
}

.sender-section {
  margin-bottom: 20px;
}

.log-sidebar {
  background-color: #ffffff;
  margin: 16px 16px 16px 0;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border-left: 4px solid #409eff;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .content-container {
    flex-direction: column;
  }
  
  .log-sidebar {
    width: 100% !important;
    height: 300px;
    margin: 0 16px 16px 16px;
  }
  
  .main-content {
    margin: 16px 16px 0 16px;
  }
}

@media (max-width: 768px) {
  .header {
    padding: 0 10px;
  }
  
  .title {
    font-size: 18px;
  }
  
  .main-content,
  .log-sidebar {
    margin: 8px;
  }
}
</style> 